import { Component, OnInit} from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { myProjectService } from '../../service/myProject.service';
import { isBindPNService } from '../../service/isBindPN.service';
import { Pagination } from  '../../lib/pagination';
declare let $:any;
@Component({
    selector: 'game',
    templateUrl: '../../../views/my_project/myProject.html',
    styleUrls: ['../../../css/my_project/myProject.css'],
    providers: [
        myProjectService,
        Pagination,
        isBindPNService
    ]
})
export class myProjectComponent {
    public games : any = [];
    public games1 : any = [];
    public resource_url:any;
    public id : number;
    private tab : string;
    private param : any = {};
    public selectedPage : any;
    public games_loading : any =[];
    public isNav:any = true;
    public games_info : any = [];
    constructor(
        private myProjectService : myProjectService,
        private Pagination : Pagination,
        private router : Router,
        private route: ActivatedRoute,
        private isB : isBindPNService
    ) {

        let self : any = this;
        self.isB.isId();
        self.isB.isBindPN();
        self.route.params.subscribe((params : any)=> {
            for (let key in params) {
                self.param[key] = params[key];
            }
        });
        if (self.param.page == undefined) {
            self.param.page = 1;
        }
        if (self.param.limit == undefined) {
            self.param.limit = 8;
        }
        self.games.list = [];
        self.getGameList(self.param.page);
        //加载上拉加载
        self.pullLoading();
        let item = $("li[name='item']:first-child a");
        console.log(item);
        $.cookie("home","2")
    }

    public changeIsnav(){
        $(".mask").toggle();
    }
    public noEdit(){
        alert("游戏尚未编辑，请在电脑上进行编辑操作!");
    }
    /**
     * 公众号的游戏列表
     *
     * @param number: id 公众号ID
     * @param number: page 分页ID
     * @param number: limit 显示记录数
     */
    public getGameList(index:any) {
        let self : any = this;
        self.id = $.cookie("id");
        let data : any = {
            member_id: self.id,
            page: self.param.page,
            limit: self.param.limit
            // limit: index
        };
        self.myProjectService.getMyProjectList({
            data: data,
            success: function (result : any) {
                self.games1.list = result.data;
                for( var i = 0; i < self.games1.list.length; i++){
                    self.games.list.push( self.games1.list[i] );
                }
                console.log(  self.games.list );
                self.games.count = result.count;
                self.resource_url = result.resource_url;
                self.games.pagination = self.Pagination.getPage(self.param.page,result.page_count);
                console.log(self.games.pagination.count);
                self.selectedPage = self.games.pagination.current;
                /*self.games_loading.push(result);
                alert(self.games_loading.length);
                console.log(self.games_loading);
                */
               // alert(self.games.list.length);
               //  for (let key in result.data) {
               //      let val = result.data[key];
               //      let data = {};
               //      data['title']=val['title'];
               //     // alert(data['title']);
               //      self.games_loading.push(data);
               //      //alert(self.games_loading.length);
               //  }
               //  //先加载一部分
               //  self.loadingPart();
            }
        });
    }

    public previousPage() {
        var self:any = this;
        const current : number = parseInt(self.param.page);
        const page : number = self.Pagination.previousPage(current);
        self.param.page = page;
        self.selectedPage = page;
        self.router.navigate(['/my_project',{page:page}]);
        self.getGameList();
    }

    public nextPage() {
        var self:any = this;
        const count : number = parseInt(self.games.pagination.count);
        const current : number = parseInt(self.param.page);
        const page : number = this.Pagination.nextPage(current, count);
        self.param.page = page;
        self.selectedPage = page;
        self.router.navigate(['/my_project',{page:page}]);
        self.getGameList();
    }

    public locationPage(page : number) {
        var self:any = this;
        self.param.page = page;
        self.selectedPage = page;
        self.router.navigate(['/my_project',{page:page}]);
        self.getGameList();
    }

    public pullLoading() {
        var self:any = this;
        document.onscroll = function() {
                //if (document.body.scrollTop + document.body.clientHeight >= document.body.scrollHeight) {
            if ($(document).scrollTop() >= $(document).height() - $(window).height())  {
                //到达底部
                //$('.addMore').fadeIn(50);
                $(document).scrollTop();
                //alert("滚动条已经到达底部为" + $(document).scrollTop());
                self.loadingPart();

            }
        }
    }
    public loadingPart() {
       let self = this;
       self.nextList();
    }

    public nextList() {
        var self:any = this;
        self.param.page++;
        console.log(self.games.pagination.count);
        if( self.param.page > self.games.pagination.count ){
            $('#addMore').html('已经到底了').delay(3000).hide(50);
        }else {
            self.getGameList();
        }
    }

    public pageScroll(){
        var speed=200;//滑动的速度
        $('body,html').animate({ scrollTop: 0 }, speed);
        return false;
    }










}






    